package us.icitap.timsportal.management;

import java.util.Arrays;
import java.util.List;

import us.icitap.timsportal.entity.OrgStructure;
import us.icitap.timsportal.entity.User;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.util.ListData;
import us.icitap.timsportal.web.custom.OrgStructureSelect;
import us.icitap.timsportal.web.custom.PortalDateField;
import us.icitap.timsportal.web.custom.PortalForm;
import us.icitap.timsportal.web.custom.PTextField;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;

public class UserForm extends PortalForm {	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4249968454556311154L;
	
	private static final Object[] NATURAL_COL_ORDER = new Object[] {
		"username", "email", "startDate", "endDate", "active", "name", "surname", "function", "baseStructure", "roles" };
	private List<Object> orderedProperties = Arrays.asList(NATURAL_COL_ORDER);
	private RoleComponent roleComponent = new RoleComponent();
	private User user;
	private boolean noOfficer;
	private final OrgStructureSelect orgStrSelect = new OrgStructureSelect(TimsPortalLabels.getLabel("label.orgStructure"));
	
	public UserForm(){
		setWriteThrough(true);

		setFormFieldFactory(new DefaultFieldFactory(){
			private static final long serialVersionUID = 3650397984032334921L;
						
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				if(propertyId.equals("roles")){
					return roleComponent;
				}
				if(propertyId.equals("baseStructure")){
					return orgStrSelect;
				}
				
				if (propertyId.equals("username")){					
					PTextField field = new PTextField(TimsPortalLabels.getLabel("label.username"));
					field.setRequired(true);
					field.setRequiredError(TimsPortalLabels.getLabel("msg.userNotEmpty"));
					field.setMaxLength(30);
					field.setWidth("200px");
					field.setNullRepresentation("");
					field.setImmediate(true);
					return field;
				} else if(propertyId.equals("startDate")){					
					PortalDateField dateTime = new PortalDateField(TimsPortalLabels.getLabel("label.startDate"), DateField.RESOLUTION_DAY);
					dateTime.setRequired(true);
					dateTime.setRequiredError(TimsPortalLabels.getLabel("msg.startDateNotEmpty"));
					dateTime.setWidth("200px");
					dateTime.setDateFormat("dd.MM.yyyy");
					dateTime.setStyleName("timsportal");
					dateTime.setImmediate(true);
					return dateTime;
				} else if(propertyId.equals("endDate")){					
					PortalDateField dateTime = new PortalDateField(TimsPortalLabels.getLabel("label.endDate"), DateField.RESOLUTION_DAY);
					dateTime.setWidth("200px");
					dateTime.setDateFormat("dd.MM.yyyy");
					dateTime.setImmediate(true);
					return dateTime;
				} else if(propertyId.equals("active")){
					CheckBox cb = new CheckBox(TimsPortalLabels.getLabel("label.locked"));
					return cb;
				} else if(propertyId.equals("email")){
					PTextField field = new PTextField(TimsPortalLabels.getLabel("label.email"));
					field.setRequired(true);
					field.addValidator(new EmailValidator(TimsPortalLabels.getLabel("msg.emailValidator")));
//					field.setMaxLength(30);
					field.setWidth("200px");
					field.setNullRepresentation("");
					field.setImmediate(true);
					return field;
				} else if (propertyId.equals("name")){
					PTextField field = new PTextField(TimsPortalLabels.getLabel("label.name"));
					field.setRequired(true);
					field.setRequiredError(TimsPortalLabels.getLabel("msg.userNotEmpty"));
					field.setMaxLength(30);
					field.setWidth("200px");
					field.setNullRepresentation("");
					field.setImmediate(true);
					return field;
				} else if (propertyId.equals("surname")){
					PTextField field = new PTextField(TimsPortalLabels.getLabel("label.surname"));
					field.setRequired(true);
					field.setRequiredError(TimsPortalLabels.getLabel("msg.userNotEmpty"));
					field.setMaxLength(30);
					field.setWidth("200px");
					field.setNullRepresentation("");
					field.setImmediate(true);
					return field;
				} else if (propertyId.equals("function")){
					PTextField field = new PTextField(TimsPortalLabels.getLabel("label.function"));
					field.setRequired(true);
					field.setRequiredError(TimsPortalLabels.getLabel("msg.userNotEmpty"));
					field.setMaxLength(30);
					field.setWidth("300px");
					field.setNullRepresentation("");
					field.setImmediate(true);
					return field;				
				}
				return null;
			}
		});
	}
	
	public void setUser(User user){
		this.user = user;
		setItemDataSource(new BeanItem<User>(this.user), orderedProperties);
		roleComponent.setRoles(this.user.getRoles());
	}
	
	public User getUser(){
		user.setRoles(roleComponent.getRoles());
		return user;
	}
	
	public void setNoOfficer(boolean noOfficer){
		this.noOfficer = noOfficer;
		/*Perdoruesi ka oficer,prandaj emer,mbiemer,funksion,strukture bahen null */
		if(!noOfficer){
			setOfficerFieldsReadOnly(false);
			((PTextField)getField("name")).setValue("N/A");
			((PTextField)getField("surname")).setValue("N/A");
			((PTextField)getField("function")).setValue("N/A");
			orgStrSelect.setValue(null);
			
		}
		getField("name").setRequired(noOfficer);
		getField("surname").setRequired(noOfficer);
		getField("function").setRequired(noOfficer);
		orgStrSelect.setRequired(noOfficer);
		setOfficerFieldsReadOnly(!noOfficer);
	}
	
	public boolean isNoOfficer(){
		return noOfficer;		
	}
	
	private void setOfficerFieldsReadOnly(boolean readOnly){
		getField("name").setReadOnly(readOnly);
		getField("surname").setReadOnly(readOnly);
		getField("function").setReadOnly(readOnly);
		orgStrSelect.setReadOnly(readOnly);
	}
	
	@Override
	public void attach(){
		super.attach();
		
		BeanItemContainer<OrgStructure> orgStructureContainer = new BeanItemContainer<OrgStructure>(OrgStructure.class);
		orgStructureContainer.addAll(ListData.getListOS(getApplication()));
		orgStrSelect.setContainerDataSource(orgStructureContainer);	
	}
}
